using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Security;
using System.Runtime.Serialization;

namespace TST.SharePoint.Shared
{
    /// <summary>
    /// WebPart that displays data from numerous data sources in a grid.
    /// WebPart is built as ASP.NET webpart, but targeted at the WSS/MOSS platform.
    /// </summary>
    [ToolboxData("<{0}:ContentBySearch runat=server></{0}:ContentBySearch>")]
    public class BaseDataWebPart : System.Web.UI.WebControls.WebParts.WebPart //MultiValuedUserInputFilterWebPart
    {
        private Datasources _datasources;
        private GridDatasource _currentDatasource;
        private string _datasourceConfiguration;
        private DatasourceConfiguration _currentConfiguration;

        /// <summary>
        /// Construction.
        /// </summary>
        public BaseDataWebPart()
        {
            _datasources = new Datasources();
        }

        /// <summary>
        /// Configuration object; stores the configuration of the webpart by the user.
        /// Object is Serializable, to save/restore configuration using the webpart framework.
        /// </summary>
        [WebBrowsable(false)]
        [Personalizable(PersonalizationScope.Shared)]
        public string Configuration
        {
            get {
                if (_datasourceConfiguration == null)
                    return string.Empty;
                return _datasourceConfiguration; 
            }
            set
            {
                if (_datasourceConfiguration != value)
                {
                    _datasourceConfiguration = value;
                    _currentConfiguration = null;
                    SetCurrentDataSource(); //TST
                    if (_currentDatasource != null && (!string.IsNullOrEmpty(_datasourceConfiguration)))
                    {
                        _currentConfiguration = DatasourceConfiguration.ReadXml(Configuration);
                        _currentDatasource.Configuration = _currentConfiguration;
                        //TST - twice??
                        _currentDatasource.ReadConfiguration(_currentConfiguration);
                        ConfigurationChanged();
                    }
                }
            }
        }

        public static string GetOpenToolPaneText(string message, string controlID)
        {
            return string.Format("{3}. Click <a href=\"javascript:MSOTlPn_ShowToolPaneWrapper('{0}','{1}','{2}');\">here</a> to open the toolpane.", 1, 129, controlID, message);
        }

        protected virtual void ConfigurationChanged()
        {
        }

        /// <summary>
        /// Get the list of available datasources
        /// </summary>
        public Datasources DataSources
        {
            get { return _datasources; }
        }

        /// <summary>
        /// Get the current datasource
        /// </summary>
        public GridDatasource CurrentDataSource
        {
            get 
            {
                if (_currentDatasource == null)
                {
                    SetCurrentDataSource();
                }
                return _currentDatasource;
            }
        }

        public DatasourceConfiguration CurrentConfiguration
        {
            get
            {
                if (_currentConfiguration == null)
                {
                    _currentConfiguration = DatasourceConfiguration.ReadXml(Configuration);
                }
                return _currentConfiguration;
            }
        }

        /// <summary>
        /// Get the selected datasource from the webpart properties and setup the datasource.
        /// </summary>
        protected void SetCurrentDataSource()
        {
            // Check if a datasource ID was stored in the properties.
            if ((_currentDatasource == null) && (!string.IsNullOrEmpty(CurrentConfiguration.DatasourceID)) && (CurrentConfiguration.DatasourceID != Guid.Empty.ToString("B")))
            {
                // Check if datasource is available.
                if (DataSources[CurrentConfiguration.DatasourceID] == null)
                {
                    throw new DataWebPartException(string.Format("Datasource {0} is not available.", CurrentConfiguration.DatasourceID));
                }

                // Set the current datasource.
                _currentDatasource = DataSources[CurrentConfiguration.DatasourceID];
                _currentDatasource.Configuration = CurrentConfiguration;

            }
        }
    }

    public class DataWebPartException : Exception
    {
        public DataWebPartException() : base() { }
        public DataWebPartException(string message) : base(message) { }
        protected DataWebPartException(SerializationInfo info, StreamingContext context) : base(info, context) { }
        public DataWebPartException(string message, Exception innerException) : base(message, innerException) { }
    }
}
